package Management;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class AdjusterMachine {
	private boolean 	isRight;			//Check the fileName "if or else the number of "[" and "]" is equal;
	private boolean 	isAdjusted;			//Check the filename by rule;
	private String[] 	fileNameKey;		//the String[] of fileName;
	private String[] 	directionaryName;	//动画的文件名里面以目录命名的文件名
	private String[] 	keyOfRule;			//the keyWord of Rule
	private int			index;				//the index of rule in ruleList
	private File		animeFile;			//the File of animeFile


	/**
	 * Constructor
	 * @param index
	 * @param fileName
	 */
	public AdjusterMachine (int index, String fileName) {
		this(index, new File(fileName));
	}

	/**
	 * Constructor
	 * @param index			used when using "RuleList"
	 * @param fileName		animation's File
	 */
	public AdjusterMachine(int index, File animeFile) {
		//initial the this.index and this.fileName;
		this.index 		= index;
		this.animeFile 	= animeFile;
		
		//get the rules;
		Rules r 			= new RuleList().getList().get(index);

		//get the keyWord of rule
		int starNumber			= r.getStarNumber();
		int numberOfKeyOfRule   = r.getKey().length - starNumber - 1;
		keyOfRule = new String[numberOfKeyOfRule];
		for (int i = 0; i < numberOfKeyOfRule; i++) {
			keyOfRule[i] = r.getKey()[i+ starNumber + 1];
//			System.out.println("keyOfRule:"+keyOfRule[i]);
		}
		
		//get the MainPath from the rule;
		String mainPath 	= r.getMainPath();
//		System.out.println("mainPath:"+mainPath);
		char[] mpath		= mainPath.toCharArray();
		
		//get the path of the animeFile ;
		String animePath	= animeFile.getParent();
//		System.out.println("animePath:"+animePath);
		char[] apath		= animePath.toCharArray();	
		
		//get the 文件名中的目录名;
		if (mainPath.length() < animePath.length()) {
			String dname 	 = String.valueOf(apath, mpath.length + 1, apath.length - mpath.length - 1);
			directionaryName = speratString(dname);
//			System.out.println(dname);
		}
		else {
			directionaryName = new String[1];
			directionaryName[0] = "";
		}

		
		//get the file name without ".rmvb";
		String[] name 		= animeFile.getName().split("[.]");
		String fileName 	= name[0];
		
		//set the key word;
		setKey(fileName);
		
		//check the file if it is adjusted by the number of keyword;
		//first :it must be right first('[' == ']';
		if (isRight) {
			int numberOfKeyOfAnime  = fileNameKey.length - directionaryName.length;
			//next : numberOfKeyOfRule == numberOfKeyOfAnime;
			if (numberOfKeyOfRule == numberOfKeyOfAnime) {
				isAdjusted = true;
				for (int i = 0; i < directionaryName.length; i++) {
					//last :first part of fileNameKey should equal directionaryName;
					if (!fileNameKey[i].equalsIgnoreCase(directionaryName[i])) {
						isAdjusted = false;
						break;
					}
				}
			}
			else {
				isAdjusted = false;
			}
		}
		else {
			isAdjusted = false;
		}
	}

	/**
	 * sperate string with '\';
	 * @param dname
	 * @return
	 */
	public String[] speratString(String dname) {
		String[] directionaryName;
		char[] n = dname.toCharArray();
		List<Integer> in = new ArrayList<Integer>();

		//get the index of '\';
		for (int j = 0; j < n.length; j++) {
			if (n[j] == '\\') {
				in.add(j);
			}
		}

		//initial;
		int foraward = 0;
		int last;
		directionaryName = new String[in.toArray().length + 1];
		
		//after initial,get the directionaryName by index and n
		for (int i = 0; i < in.toArray().length + 1; i++) {
			
			//make the index of d's end
			if (i == in.toArray().length) {
				last = n.length + 1;
			}else {
				last = in.get(i) + 1;
			}
			//System.out.println("last:"+last);
			
			directionaryName[i] = String.valueOf(n, foraward, last - foraward - 1);
//			System.out.println("directionaryName:" + directionaryName[i]);
			
			//make the index of d's start
			if (i == in.toArray().length) {
				//do nothing!
			}else {
				foraward = in.get(i) + 1;
			}
			//System.out.println("foraward:"+foraward);
		}
		return directionaryName;
	}
	
	/**
	 * Check the fileName "if or else the number of "[" and "]" is equal;
	 * and get the KeyWord from the fielName;
	 * for example:
	 * 				[google][dsfsf][asda][asdas]
	 * 				KeyWord is : 'google'dsfsf'asda'asdas'
	 * @param fileName
	 */
	public void setKey(String fileName) {
		char[] c = fileName.toCharArray();
		List<Integer> start = new ArrayList<Integer>(); 
		List<Integer> end = new ArrayList<Integer>();
		int i = 0;
		for (char ch: c) {
			if(ch == '[') {
				start.add(i);
			}
			else if(ch == ']') {
				end.add(i);
			}
			i++;
		}
		
		//check it ,if it is right by the number of "[" and "]";
		if (start.toArray().length == end.toArray().length) {
			this.isRight = true;
		}else {
			this.isRight = false;
		}
		
		//get key[]
		if (isRight == true) {
			fileNameKey = new String[start.toArray().length];
			for (int j = 0; j < start.toArray().length; j++) {
				int iStart 	= (int) start.get(j);
				int iEnd	= (int) end.get(j);
				fileNameKey[j] = String.valueOf(c, iStart + 1, iEnd - iStart - 1);
//				System.out.println("key:"+key[j]);
			}
		}

	}

	/**
	 * isRight
	 * @return
	 */
	public boolean isRight() {
		return isRight;
	}

	/**
	 * setRight
	 * @param isRight
	 */
	public void setRight(boolean isRight) {
		this.isRight = isRight;
	}

	/**
	 * getKey
	 * @return
	 */
	public String[] getKey() {
		return fileNameKey;
	}
	
	/**
	 * getAdjustedNameArray
	 * @return    if isAdjusted return the "bufferredName[0] = "It has be Adjusted!""
	 * 			  if !isAdjusted return "String[] bufferredName";
	 */
	public String[] getAdjustedNameArray() {
		if (!isAdjusted) {
			String[] bufferredName = new String[keyOfRule.length + directionaryName.length];
			
			//make the directionaryName and keyOfRule become bufferredName
			for (int i = 0; i < directionaryName.length; i++) {
				bufferredName[i] = directionaryName[i];
//				System.out.println("bufferredName:" + bufferredName[i]);
			}
			for (int i = 0; i < keyOfRule.length; i++) {
				bufferredName[i + directionaryName.length] = keyOfRule[i];
//				System.out.println("keyOfRule:" + keyOfRule[i]);
			}
			
			return bufferredName;
		}
		else {
			String[] bufferredName = new String[1] ;
			bufferredName[0] = "It has be Adjusted!";
			return bufferredName;
		}
	}
	
	/**
	 * getAdjustedName()
	 * @return  if isAdjusted return the "rightName = "It has be Adjusted!""
	 * 			if !isAdjusted return "String rightName";
	 */
	public String getAdjustedName() {
		if (!isAdjusted) {
			String rightName = "";
			String[] bufferredName = getAdjustedNameArray();
			
			//make the String[] become a String;
			for (String s: bufferredName) {
				if (!s.equals("")) {
					rightName = rightName + "[" + s +"]";
				}
			}
			rightName = rightName.trim();
//			System.out.println("rightName:" + rightName);
			return rightName;
		}
		else {
			String rightName = "It has be Adjusted!";
			return rightName;
		}
	}
	
	/**
	 * rename the File ,this field is not complete
	 */
	public boolean reNameFile(String s) {
		Rules r = new RuleList().getList().get(index);

		if (!animeFile.isDirectory()) {
			String[] name 		= animeFile.getName().split("[.]");
			String fileType 	= name[name.length - 1];
			String path = null;
			if (animeFile.getParent().endsWith(File.separator)) {
				path = animeFile.getParent() + s + "." + fileType;
			}
			else {
				path = animeFile.getParent() + File.separator + s + "." + fileType;
			}
			animeFile.renameTo(new File(path));
			return true;
		}
		else if (animeFile.isDirectory()) {
			String path = null;
			if (animeFile.getParent().endsWith(File.separator)) {
				path = animeFile.getParent() + s;
			}
			else {
				path = animeFile.getParent() + File.separator + s;
			}
			
			if (animeFile.getAbsoluteFile().equals(new File(r.getMainPath()).getAbsoluteFile())) {
				r.changeMainPathTo(path);
			}
			animeFile.renameTo(new File(path));
			return true;
		}
		else {
			return false;
		}
	}
	
	/**
	 * isAdjusted();
	 * @return
	 */
	public boolean isAdjusted() {
		if (this.animeFile.isDirectory()) {
			ArrayList<File> directory = new ArrayList<File> ();
			directory.add(this.animeFile);
			
			while(true) {
				ArrayList<File> directoryf = new ArrayList<File> ();
				for (File ff: directory) {
					for (File f: ff.listFiles()) {
						if (f.isDirectory()) {
							directoryf.add(f);
						}
						else {
							AdjusterMachine am = new AdjusterMachine(this.index, f);
							if (!am.isAdjusted) {
								return false;
							}
						}
					}
				}
				if (directoryf.isEmpty()) {
					break;
				}
				
				directory.clear();
				directory = directoryf;
			}
			return true;
		}
		else {
			return isAdjusted;
		}
	}
}
